package antlr4.myantlrtest.test.grammarsv4.protobuf3.test;

import antlr4.myantlrtest.gen.grammarsv4.protobuf3.Protobuf3Lexer;
import antlr4.myantlrtest.gen.grammarsv4.protobuf3.Protobuf3Parser;
import cn.hutool.core.io.FileUtil;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public class Protobuf3 {

    public static void main(String[] args) {

        if (args.length > 0) {
            String arg = args[0];
            String content = FileUtil.readString(arg, "UTF-8");
            System.out.println("读取文件" + arg + ",内容:\n" + content);
            CodePointCharStream input = CharStreams.fromString(content);
            System.out.println("开始解析文件");

            Protobuf3Lexer lexer = new Protobuf3Lexer(input);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            Protobuf3Parser parser = new Protobuf3Parser(commonTokenStream);
            ParseTreeWalker walker = new ParseTreeWalker();//新建一个标准的遍历器
            Protobuf3Listener listener = new Protobuf3Listener(parser);
            walker.walk(listener, parser.proto());

        } else {
            System.err.println("不推荐手写字符串，还是读取一个已经存在的文件吧");
        }
    }

}
